//
//  UIImage+Extension.swift
//  照片选择器
//
//  Created by IOS on 15/8/17.
//  Copyright © 2015年 IOS. All rights reserved.
//

import UIKit

extension UIImage {
    
    //缩放图片
    func scaleImage(width: CGFloat) -> UIImage {
        
        if size.width < width {
            return self
        }
        
        let height = size.height * width / size.width
        let s = CGSize(width: width, height: height)
        
        //使用图形上下文重新绘制图像
        //1.开启图形上下文
        UIGraphicsBeginImageContext(s)
        //2.绘图
        drawInRect(CGRect(origin: CGPointZero, size: s))
        //3.从当前上下文中拿到结果
        let result = UIGraphicsGetImageFromCurrentImageContext()
        //4.关闭当前上下文
        UIGraphicsEndImageContext()
        //5。返回结果
        return result
    }
    
}
